from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import QWidget,QSystemTrayIcon,QStackedWidget,QMainWindow,QLabel,QStyleOption,QStyle,QVBoxLayout,QHBoxLayout
from PyQt5.QtGui import QPainter,QColor,QPalette,QIcon,QCursor,QPainterPath
from abstmainwidget import *
from trayiconmenu import *
import math
import lowmainres
import trayiconmenures
import upmainres
import centerwindres
class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow, self).__init__(parent)
        self.setMinimumSize(910,610)
        self.setMaximumSize(910,610)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
        self.setWindowIcon(QIcon(":/image/360logo.ico"))
        self.mousePressed = False

        self.stackwid = QStackedWidget(self)
        self.csxfwid = CsxfWidget(self)
        self.dnqlwid = DnqlWidget(self.stackwid)
        self.yhjswid = YhjsWidget(self.stackwid)


        self.csxfwid.topwidget().btnreturn.clicked.connect(self.returnanimation)
        self.dnqlwid.topwidget().btnreturn.clicked.connect(self.returnanimation)
        self.yhjswid.topwidget().btnreturn.clicked.connect(self.returnanimation)

        self.csxfwid.btnexit.clicked.connect(self.close)
        self.dnqlwid.btnexit.clicked.connect(self.close)
        self.yhjswid.btnexit.clicked.connect(self.close)

        self.csxfwid.btnmini.clicked.connect(self.showMinimized)
        self.dnqlwid.btnmini.clicked.connect(self.showMinimized)
        self.yhjswid.btnmini.clicked.connect(self.showMinimized)

        self.stackwid.addWidget(self.csxfwid)
        self.stackwid.addWidget(self.dnqlwid)
        self.stackwid.addWidget(self.yhjswid)
        self.stackwid.setGeometry(QRect(5,5,900,600))
        self.stackwid.setCurrentIndex(0)
        self.maskmainwid = MaskMainWidget(self)
        self.maskmainwid.setGeometry(5,5,900,600)
        self.maskmainwid.upmain.btnexit.clicked.connect(self.close)
        self.maskmainwid.upmain.btnmini.clicked.connect(self.showMaximized)


        self.maskupwincloseani = QPropertyAnimation(self.maskmainwid.upmain,b"pos")
        self.masklowwincloseani = QPropertyAnimation(self.maskmainwid.lowmain,b"pos")

        self.maskupwincloseani.setStartValue(QPoint(0, 0));
        self.maskupwincloseani.setEndValue(QPoint(0, -440));
        self.maskupwincloseani.setDuration(200);

        self.masklowwincloseani.setStartValue(QPoint(0, 440));
        self.masklowwincloseani.setEndValue(QPoint(0, 900));
        self.masklowwincloseani.setDuration(200);

        self.maskupwinshowani = QPropertyAnimation(self.maskmainwid.upmain, b"pos");
        self.masklowwinshowani = QPropertyAnimation(self.maskmainwid.lowmain, b"pos");

        self.maskupwinshowani.setStartValue(QPoint(0, -440));
        self.maskupwinshowani.setEndValue(QPoint(0, 0));
        self.maskupwinshowani.setDuration(300);

        self.masklowwinshowani.setEndValue(QPoint(0, 440));
        self.masklowwinshowani.setStartValue(QPoint(0, 900));
        self.masklowwinshowani.setDuration(300);

        self.maskupwincloseani.finished.connect(self.slot_hideanimationfinished)
        self.masklowwincloseani.finished.connect(self.slot_hideanimationfinished)

        self.maskmainwid.lowmain.btn.clicked.connect(self.slot_setcurstack0)
        self.maskmainwid.lowmain.btn1.clicked.connect(self.slot_setcurstack1)
        self.maskmainwid.lowmain.btn2.clicked.connect(self.slot_setcurstack2)


        self.maskmainwid.lowmain.btn.clicked.connect(self.slot_hideanimation)
        self.maskmainwid.lowmain.btn1.clicked.connect(self.slot_hideanimation)
        self.maskmainwid.lowmain.btn2.clicked.connect(self.slot_hideanimation)

        self.systemtray = QSystemTrayIcon()
        self.traymenu = TrayIconMenu()
        self.systemtray.setContextMenu(self.traymenu)
        self.systemtray.setToolTip("360安全卫士 - 安全防护中心全面开启")
        self.systemtray.setIcon(QIcon(":/image/360logo.ico"))
        self.systemtray.show()

        self.systemtray.activated.connect(self.slot_iconIsActived)

    def slot_iconIsActived(self,reason):
        if reason == QSystemTrayIcon.Context:
            self.traymenu.setGeometry(QCursor.pos().x()-self.traymenu.width(),QCursor.pos().y()-self.traymenu.height(),self.traymenu.width(),self.traymenu.height())
        elif reason == QSystemTrayIcon.Trigger or reason == QSystemTrayIcon.DoubleClick:
            self.showNormal()
            self.activeWindow()
    def slot_hideanimationfinished(self):
        self.maskmainwid.hide()
    def slot_hideanimation(self):
        self.masklowwincloseani.start()
        self.maskupwincloseani.start()
    def returnanimation(self):
        self.maskmainwid.show()
        self.maskupwinshowani.start()
        self.masklowwinshowani.start()

    def slot_setcurstack0(self):
        self.stackwid.setCurrentIndex(0)
    def slot_setcurstack1(self):
        self.stackwid.setCurrentIndex(1)
    def slot_setcurstack2(self):
        self.stackwid.setCurrentIndex(2)
    def slot_setcurstack3(self):
        self.stackwid.setCurrentIndex(3)

    def eventFilter(self, a0: 'QObject', a1: 'QEvent'):
        super(MainWindow, self).eventFilter(a0,a1)

    def mousePressEvent(self, a0: QtGui.QMouseEvent):
        rc = QRect(5,5,900,150)
        if rc.contains(self.mapFromGlobal(QCursor.pos()))==True:
            if a0.button() == QtCore.Qt.LeftButton:
                self.windowpos = self.pos()
                self.mousepos = a0.globalPos()
                self.mousePressed = True

    def mouseMoveEvent(self, a0: QtGui.QMouseEvent):
        if self.mousePressed:
            self.move(self.windowpos +(a0.globalPos() - self.mousepos))

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        if a0.button() == QtCore.Qt.LeftButton:
            self.mousePressed = False

    def paintEvent(self, a0: QtGui.QPaintEvent):
        path = QPainterPath()
        path.setFillRule(QtCore.Qt.WindingFill)
        path.addRect(9,9,self.width()-18,self.height()-18)

        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing,True)
        painter.fillPath(path,QBrush(QtCore.Qt.white))


        color= QColor(0,0,0,50)
        for i in range(9):
            patht = QPainterPath()
            patht.setFillRule(QtCore.Qt.WindingFill)
            patht.addRect(9-i,9-i,self.width()-(9-i)*2,self.height()-(9-i)*2)
            color.setAlpha(150-math.sqrt(i)*50)
            painter.setPen(color)
            painter.drawPath(patht)






















